

#import "BIDHistoryTableViewController.h"
#import "BIDAppDelegate.h"

@interface BIDHistoryTableViewController ()

@end

@implementation BIDHistoryTableViewController{
    NSInteger currentCellIndex;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    // get list of devs from Core Data
    BIDAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Developer"];
    NSError *error;
    self.developers = [context executeFetchRequest:request error:&error];
    if (self.developers == nil){
        NSLog(@"Data in local storage is empty !");
        return;
    }
    self.lastCommits = [[NSMutableArray alloc] init];
    for (NSManagedObject *dev in self.developers){
        NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"revision" ascending:NO];
        NSArray *descriptors = [NSArray arrayWithObject:descriptor];
        NSArray *commits = [[dev valueForKey:@"developerCommits"] sortedArrayUsingDescriptors:descriptors];
        [self.lastCommits addObject:commits[0]];
        //NSLog(@"last revision: %@", [commits[0] valueForKey:@"revision"]);
    }
    // get revision and date from last commit of each dev
    [self.tableView reloadData];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [self.developers count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"disclosureCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    //TODO: below code should be move to viewDidLoad()
    CGRect nameLabelRect = CGRectMake(3, 0, 320, 24);
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:nameLabelRect];
    nameLabel.text = [self.developers[indexPath.row] valueForKey:@"name"];
    nameLabel.font = [UIFont boldSystemFontOfSize:16];
    [cell.contentView addSubview:nameLabel];
    CGRect revisionLabelRect = CGRectMake(3, 24, 160, 20);
    UILabel *revisionLabel = [[UILabel alloc] initWithFrame:revisionLabelRect];
    revisionLabel.text = [NSString stringWithFormat:@"%@",[self.lastCommits[indexPath.row] valueForKey:@"revision"]];
    revisionLabel.font = [UIFont boldSystemFontOfSize:10];
    [cell.contentView addSubview:revisionLabel];
    CGRect dateLabelRect = CGRectMake(163, 24, 320, 20);
    UILabel *dateLabel = [[UILabel alloc] initWithFrame:dateLabelRect];
    dateLabel.text = [self.lastCommits[indexPath.row] valueForKey:@"dateOfCommit"];
    dateLabel.font = [UIFont boldSystemFontOfSize:10];
    [cell.contentView addSubview:dateLabel];
    //NSLog(@"last commits date: %@", [self.lastCommits[indexPath.row] valueForKey:@"dateOfCommit"]);
    //NSLog(@"last revision: %@", [self.lastCommits[indexPath.row] valueForKey:@"revision"]);
    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    currentCellIndex = indexPath.row;
}
// Set proper name to SettingsDetailTalbeView
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    UITableViewController *destination = segue.destinationViewController;
    //destination.title = [[sender valueForKey:@"textLabel"] valueForKey:@"text"];
    destination.title = [self.developers[currentCellIndex] valueForKey:@"name"];
    //transfer data to destination
}
@end
